package 富婆梦.第一阶段.第六周.作业.俄罗斯转盘;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.LinkedList;

/**
 * @ProjectName: java-learning
 * @Package: 富婆梦.第一阶段.第六周.作业.俄罗斯转盘
 * @ClassName: Preserve
 * @Author: hemengjin
 * @Description: 保存当前情况的类
 * @Date:2021/8/28 22:35
 * @Version: 1.0
 */
public class Preserve {
    static int count = 0;

    public static void write(LinkedList<Player> list) {
        try {
            FileOutputStream file = new FileOutputStream("D:/tool/1.txt");
            ObjectOutputStream os = new ObjectOutputStream(file);
            os.writeObject(list);
            System.out.println("存档成功！游戏暂停！");
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static LinkedList<Player> read() {
        LinkedList list = null;
        try {
            FileInputStream file = new FileInputStream("D:/tool/1.txt");
            ObjectInputStream os = new ObjectInputStream(file);
            list = (LinkedList<Player>) os.readObject();
            System.out.println("读取成功！游戏继续！");
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public static void main(String[] args) {
        LinkedList<Player> list = new LinkedList();
        list.add(new Player("小A"));
        list.add(new Player("小B"));
        list.add(new Player("小C"));
        list.add(new Player("小D"));
        System.out.println("存档前的list：" + list);

        for (Player p : list) {
            System.out.println(p.getName() + ":" + p.isLife());
        }
        write(list);
        list = null;
        System.out.println("存档后的list：" + list);
        list = read();
        System.out.println("读取后的list：" + list);
        for (Player p : list) {
            System.out.println(p.getName() + ":" + p.isLife());
        }

    }
}
